﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace cognysWCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }

        #region BaseLine/resultstate
        //adds a baseline object.
        public void AddBaseLine(Baseline bl)
        {
            using (var context = new Rapp_DBEntities())
            {
                try
                {
                    context.Baseline.Add(bl);
                    context.SaveChanges();
                }
                catch
                {

                }
            }
        }
        //adds a resultstate object.
        public void AddResultState(ResultState rs)
        {
            using (var context = new Rapp_DBEntities())
            {
                try
                {
                    context.ResultState.Add(rs);
                    context.SaveChanges();
                }
                catch
                {

                }
            }
        }
        //gets the baseline object from the person.
        public Baseline getBaseline(Guid person)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = context.Baseline.Where(x => x.Person == person).FirstOrDefault();

                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }
        //gets the resultstate object from the person
        public ResultState getResultState(Guid person)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = context.ResultState.Where(x => x.Person == person).FirstOrDefault();

                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }
        //edit the baseline object from the person. type is used to see what test should be edited.
        public void editBaseLine(Guid person, string type, int value)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    Baseline bl = context.Baseline.FirstOrDefault(x => x.Person == person);

                    if (type == "memory")
                    {
                        bl.ScoreMemory = value;
                        context.SaveChanges();
                    }

                    if (type == "tmt")
                    {
                        bl.ScoreTMT = value;
                        context.SaveChanges();
                    }

                    if (type == "stroop")
                    {
                        bl.ScoreStroop = value;
                        context.SaveChanges();
                    }
                    
                }
                catch
                {
                    
                }
            }
        }
        //edit the resultstate object from the person. type is used to see what test should be edited.
        public void editResultState(Guid person, string type, int value)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    ResultState rs = context.ResultState.FirstOrDefault(x => x.Person == person);

                    if (type == "memory")
                    {
                        rs.StateMemory = value;
                        context.SaveChanges();
                    }

                    if (type == "tmt")
                    {
                        rs.StateTMT = value;
                        context.SaveChanges();
                    }

                    if (type == "stroop")
                    {
                        rs.StateStroop = value;
                        context.SaveChanges();
                    }

                }
                catch
                {

                }
            }
        }

        #endregion //BaseLine/resultstate


        #region Stroop

        //adds a ResultStroop object.
        public void AddStroopResult(ResultStroop stroop)
        {
            using (var context = new Rapp_DBEntities())
            {
                try
                {
                    context.ResultStroop.Add(stroop);
                    context.SaveChanges();
                }
                catch
                {

                }
            }
        }
        //gets all ResultStroop from one person.
        public List<ResultStroop> getStroopResult(Guid personID)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = context.ResultStroop.Where(x => x.Person == personID).ToList();

                    return result;
                }
                catch
                {
                    return null;
                }
            }


        }
        //gets the average score from all ResultStroops.
        public string getStroopAvg()
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = context.ResultStroop.Average(x => x.Score);

                    return result.ToString();
                }
                catch
                {
                    return null;
                }
            }
        }
        //gets the highest score from resultStroop.
        public string getStroopTop()
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = context.ResultStroop.Max(x => x.Score);

                    return result.ToString();
                }
                catch
                {
                    return null;
                }
            }
        }
        #endregion //stroop

        #region PRiC

        //saves a ResultPric object.
        public void AddPricResult(ResultPRiC pric)
        {
            using (var context = new Rapp_DBEntities())
            {
                try
                {
                    context.ResultPRiC.Add(pric);
                    context.SaveChanges();
                }
                catch
                {
                }
            }
        }
        
        //gets a single date from each registered Pric forms for a person.
        public List<ResultPRiC> getPricRegDates(Guid person)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result =
                        context.ResultPRiC.Where(x => x.Person == person)
                               .GroupBy(r => r.regDate)
                               .Select(g => g.FirstOrDefault())
                               .ToList();

                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }
        //gets the questions from the PRIC table.
        public List<PRIC> getPricQuestions()
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    return context.PRIC.ToList();
                }
                catch
                {
                    return null;
                }
            }
        }
        //gets all results from a single form, identified by date and personID.
        public List<ResultPRiC> getUserSingleResultPric(string date, Guid personID)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = context.ResultPRiC.Where(x => x.regDate == date && x.Person == personID).ToList();
                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }

        #endregion // pric

        #region Person

        //gets list of persons where name contains the string parameter.
        public List<Person> getPersons(string name)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;

                try
                {
                    var result = context.Person.Where(x => x.Firstname.Contains(name) || x.Lastname.Contains(name)).ToList();
                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }
        //gets list of persons where firstname, lastname and socialnumber contains the string parameter.
        public List<Person> getPersonsByNameAndPersonal(string FirstName, string LastName, string Personal)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;

                try
                {
                    var result = context.Person.Where(x => x.Firstname.Contains(FirstName) && x.Lastname.Contains(LastName) && x.SocialNumber.Contains(Personal)).ToList();
                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }
        //gets a person with the parameter id.
        public Person getPerson(Guid id)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {

                    var result = from x in context.Person
                                 where x.ID == id
                                 select x;

                    return result.ToList()[0];

                }

                catch
                {
                    return null;
                }
            }
        }
        
        //saves a person object.
        public void SavePerson(Person p)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Person.Add(p);
                context.SaveChanges();
            }
        }
        
      
        //edits a person object.
        public void editPerson(Person person)
        {
            using (var context = new Rapp_DBEntities())
            {
                var editDetails =
                from x in context.Person
                where x.ID == person.ID
                select x;

                foreach (var x in editDetails)
                {
                    x.Firstname = person.Firstname;
                    x.Lastname = person.Lastname;
                    x.SocialNumber = person.SocialNumber;

                }

                try
                {
                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    // Provide for exceptions.
                }
            }
        }

        #endregion //person

        #region testgroup

        //gets all testgroups.
        public List<Testgroup> getTestGroup()
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {

                    var result = from x in context.Testgroup
                                 select x;

                    return result.ToList();

                }

                catch
                {
                    return null;
                }
            }
        }

        //gets a testgroup with the parameter id.
        public Testgroup getTestGroupID(int id)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {

                    var result = context.Testgroup.Where(x => x.ID == id).FirstOrDefault();

                    return result;

                }

                catch
                {
                    return null;
                }
            }
        }
        //saves a testgroup object.
        public void createTestGroup(Testgroup testGroup)
        {
            using (var context = new Rapp_DBEntities())
            {
                try
                {

                    context.Testgroup.Add(testGroup);
                    context.SaveChanges();

                }
                catch
                {

                }
            }
        }
        //deletes a testgroup object.
        public void RemoveTestGroup(Testgroup testGroup)
        {
            using (var context = new Rapp_DBEntities())
            {
                try
                {
                    var deleteDetails =
                    from x in context.Testgroup
                    where x.ID == testGroup.ID
                    select x;

                    foreach (var x in deleteDetails)
                    {
                        context.Testgroup.Remove(x);
                    }

                    try
                    {
                        context.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        // Provide for exceptions.
                    }


                }
                catch
                {

                }
            }
        }
        //edits a testgroup object.
        public void editTestGroup(Testgroup testGroup)
        {
            using (var context = new Rapp_DBEntities())
            {
                var editDetails =
                from x in context.Testgroup
                where x.ID == testGroup.ID
                select x;

                foreach (var x in editDetails)
                {
                    x.Name = testGroup.Name;
                    x.Description = testGroup.Description;
                }

                try
                {
                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);

                }
            }
        }

        #endregion //testgroup

        #region QoR

        //saves a resultQor object.
        public void saveQor(ResultQoR qor)
        {
            using (var context = new Rapp_DBEntities())
            {
                try
                {
                    context.ResultQoR.Add(qor);
                    context.SaveChanges();

                }
                catch
                {
                }
            }
        }
        //gets all questions from the qor table.
        public List<QOR> getQorQuestions()
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = context.QOR.ToList();

                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }
        //gets a single date from each registered Qor forms for a person.
        public List<ResultQoR> getQorRegDates(Guid person)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result =
                        context.ResultQoR.Where(x => x.Person == person)
                               .GroupBy(r => r.regDate)
                               .Select(g => g.FirstOrDefault())
                               .ToList();
                               
                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }

        //gets a single result from a user.
        public List<ResultQoR> getUserSingleResultQoR(string date, Guid personID)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = context.ResultQoR.Where(x => x.regDate == date && x.Person == personID).ToList();
                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }
        //gets all ResultQors.
        public List<ResultQoR> getAllResultsQor()
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;

                try
                {
                    var result = context.ResultQoR.ToList();

                    return result;
                }
                catch
                {
                    return null;
                }
            }   
        } 


        #endregion // Qor

        #region memory

        //saves a ResultMemory object.
        public void AddMemoryResult(ResultMemory memory)
        {
            using (var context = new Rapp_DBEntities())
            {
                
                try
                {
                    context.ResultMemory.Add(memory);
                    context.SaveChanges();
                }
                catch
                {

                }
            }
        }
        //gets all ResultMemorys from a person.
        public List<ResultMemory> getMemoryResult(Guid personID)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = context.ResultMemory.Where(x => x.Person == personID).ToList();

                    return result;
                }
                catch
                {
                    return null;
                }
            }            
        } 
        //gets the average score from all ResultMemorys.
        public string getMemoryAvg()
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = context.ResultMemory.Average(x => x.Score);

                    return result.ToString();
                }
                catch
                {
                    return null;
                }
            }  
        }
        //gets the highest score from all resultMemorys
        public string getMemoryTop()
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = context.ResultMemory.Max(x => x.Score);

                    return result.ToString();
                }
                catch
                {
                    return null;
                }
            }  
        }



        #endregion //memory

        #region TMT
        //saves a resultTMT object.
        public void addTmtResult(ResultTMT tmt)
        {
            using (var context = new Rapp_DBEntities())
            {
                try
                {
                    context.ResultTMT.Add(tmt);
                    context.SaveChanges();
                }
                catch
                {

                }
            }
        }
        //gets all ResultTMTs from a person.
        public List<ResultTMT> getTMTResult(Guid personID)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = context.ResultTMT.Where(x => x.Person == personID).ToList();

                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }
        //gets the average score from all ResultTMTs.
        public string getTMTAvg()
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = context.ResultTMT.Average(x => x.Score);

                    return result.ToString();
                }
                catch
                {
                    return null;
                }
            }
        }

        //gets the highest score from all ResultTMTs.
        public string getTMTTop()
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = context.ResultTMT.Max(x => x.Score);

                    return result.ToString();
                }
                catch
                {
                    return null;
                }
            }
        }

        #endregion //TMT

        #region personTestgroup

        /// <summary>
        /// method adds a person to the selected testgroup
        /// </summary>
        /// <param name="personID"></param>
        /// <param name="testgroupID"></param>
        public void addPersonToTestgroup(Guid personID, int testgroupID)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var person = context.Person.Include("Testgroup").Where(x => x.ID == personID).FirstOrDefault();

                    if (person.Testgroup.Contains(context.Testgroup.FirstOrDefault(x => x.ID == testgroupID)))
                    {
                        //already in group
                    }
                    else
                    {
                        Testgroup tg = context.Testgroup.FirstOrDefault(x => x.ID == testgroupID);
                        person.Testgroup.Add(tg);
                        context.SaveChanges();
                    }
                }
                catch
                {

                }
            }
        }

        /// <summary>
        /// removes person from the selected testgroup
        /// </summary>
        /// <param name="personID"></param>
        /// <param name="testgroupID"></param>
        public void deletePersonFromTestgroup(Guid personID, int testgroupID)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var person = context.Person.Include("Testgroup").Where(x => x.ID == personID).FirstOrDefault();
                    Testgroup tg = context.Testgroup.FirstOrDefault(x => x.ID == testgroupID);

                    if (person.Testgroup.Contains(tg))
                    {
                        person.Testgroup.Remove(tg);
                        context.SaveChanges();
                    }
                    else
                    {

                    }
                }
                catch
                {

                }
            }
        }

        /// <summary>
        /// return the names of the testgroup that a person is a member of
        /// </summary>
        /// <param name="personID"></param>
        /// <returns></returns>
        public List<Testgroup> getTestgroupMemberhip(Guid personID)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var result = from t in context.Testgroup
                                 from p in t.Person
                                 where p.ID == personID
                                 select t;
                    return result.ToList();
                }
                catch
                {
                    return null;
                }
            }
        }

        public List<Testgroup> getTestgroupNotMember(Guid personID)
        {
            using (var context = new Rapp_DBEntities())
            {
                context.Configuration.ProxyCreationEnabled = false;
                try
                {
                    var person = context.Person.Include("Testgroup").Where(x => x.ID == personID).FirstOrDefault();
                    var list = person.Testgroup.Select(x => x.ID).ToList();
                    var result = context.Testgroup.Where(x => !list.Contains(x.ID)).ToList();
                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }

        #endregion //personTestgroup
    }

}
